<?php

namespace app\index\service;

use app\common\ConfigBase;
use app\index\model\UserModel;
use think\Db;

class QueueService
{
    static public function getQueue($param){
        $db=Db::table('queue_list');
        !empty($param['module']) && $db->where('module', '=', $param['module']);
        if(ROLE_ID==1){
            !empty($param['operator']) && $db->where('operator', '=', $param['operator']);
        }else{
           $db->where('operator', '=', UID);
        }
        if (!empty($param['operate_time'])) {
            $time = explode(' - ', $param['operate_time']);
            count($time)>1&&$db->where('operate_time', '>=', $time[0])->where('operate_time', '<=', $time[1]);
        }
        $db->order('id','desc');
        return \app\common\service\ToolService::downOrReturn($db, $param, [],"\app\index\service\QueueService::getQueueHandle");
    }
    static public function getQueueHandle($data){
        $user=UserModel::getUserById(array_column($data,'operator'));
        $module=ConfigBase::$queueName;
        $state=[
            1=>'待处理',
            2=>'处理中',
            3=>'已完成'];
        $result=[
            1=>'全部成功',
            2=>'全部失败',
            3=>'解析失败',
            4=>'部分失败'];
        foreach ($data as &$v){
            $v['operatorName']=$user[$v['operator']]??'';
            $v['moduleName']=$module[$v['module']]??'';
            $v['stateName']=$state[$v['state']]??'';
            $v['resultName']=$result[$v['result']]??'';
        }
        return $data;
    }
}